import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_getx/Locale/en_US_res.dart';
import 'package:flutter_getx/Locale/zh_CN_res.dart';
import 'package:flutter_getx/homeLocal/binding.dart';
import 'package:flutter_getx/homeLocal/view.dart';
import 'package:flutter_getx/tool/NavigatorProvider.dart';
import 'package:flutter_getx/utils/AuthMiddleware.dart';
import 'package:flutter_getx/view/home/pages/NoticeDetail.dart';
import 'package:flutter_getx/view/home/view.dart';
import 'package:flutter_getx/view/login/binding.dart';
import 'package:flutter_getx/view/login/view.dart';
import 'package:flutter_getx/view/mine/binding.dart';
import 'package:flutter_getx/view/mine/view.dart';
import 'package:flutter_getx/view/profile/binding.dart';
import 'package:flutter_getx/view/profile/view.dart';
import 'package:flutter_getx/view/tab/binding.dart';
import 'package:flutter_getx/view/tab/view.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/router_report.dart';
import 'page/home_page.dart';
import 'page/second_page.dart';
import 'page/listen_demo_page.dart';
import 'page/counter_setstate_page.dart';
import 'page/getbuilder_demo_page.dart';
import 'page/getbuilder_stateless_demo_page.dart';
import 'page/getview_demo_page.dart';
import 'page/getx_ui_demo_page.dart';
import 'page/getx_mixin_demo_page.dart';
import 'controller/home_controller.dart';
import 'controller/listen_demo_controller.dart';
import 'controller/counter_controller.dart';
import 'dart:ui' as ui;

void main() {
  // 确保Flutter绑定初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 注册 FlutterApi，使鸿蒙层能够主动调用Flutter

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      navigatorKey: NavigatorProvider.navigatorKey,
      navigatorObservers: [GetXRouterObserver()],
      title: 'GetX Demo',
      theme: ThemeData(
        useMaterial3: false,
        primarySwatch: Colors.blue,
      ),
      translations: StringRes(),
      locale: ui.window.locale,
      fallbackLocale: const Locale('en', 'US'),
      initialRoute: '/',
      getPages: [
        GetPage(
          name: '/',
          page: () => TabPage(key: UniqueKey()),
          binding: TabBinding(),
          transition: Transition.noTransition,
        ),
        GetPage(
          name: '/home',
          page: () => const HomePage(),
          binding: HomeBinding(),
        ),
        GetPage(
          name: '/mine',
          page: () => const MinePage(),
          binding: MineBinding(),
        ),
        GetPage(
          name: '/detail',
          page: () => const NoticeDetail(),
        ),

        GetPage(
          name: '/login',
          page: () => const LoginPage(),
          binding: LoginBinding(),
          middlewares: [AuthMiddleware()],
          transition: Transition.rightToLeft,
        ),

        GetPage(
          name: '/profile',
          page: () => const ProfilePage(),
          binding: ProfileBinding(),
          middlewares: [AuthMiddleware()],
        ),

        // ----------------------- gex 练习使用的理由

        // 国际
        GetPage(
          name: '/HomePageLocal',
          page: () => const HomePageLocal(),
          binding: HomeBinding(),
        ),

        //首页
        GetPage(
          name: '/text',
          page: () => const HomePageText(),
          binding: BindingsBuilder(() {
            Get.lazyPut(() => HomeController());
          }),
        ),
        GetPage(
          name: '/second',
          page: () => const SecondPage(),
          binding: BindingsBuilder(() {
            Get.lazyPut(() => HomeController());
          }),
        ),
        GetPage(
          name: '/listen_demo',
          page: () => const ListenDemoPage(),
          binding: BindingsBuilder(() {
            Get.lazyPut(() => ListenDemoController());
          }),
        ),
        GetPage(
          name: '/counter_setstate',
          page: () => const CounterSetStatePage(),
        ),
        GetPage(
          name: '/getbuilder_demo',
          page: () => const GetBuilderDemoPage(),
        ),
        GetPage(
          name: '/getbuilder_stateless_demo',
          page: () => const GetBuilderStatelessDemoPage(),
        ),
        GetPage(
          name: '/getview_demo',
          page: () => const GetViewDemoPage(),
          binding: BindingsBuilder(() {
            Get.put(CounterController());
          }),
        ),
        GetPage(
          name: '/getx_ui_demo',
          page: () => const GetxUiDemoPage(),
        ),
        GetPage(
          name: '/getx_mixin_demo',
          page: () => const GetxMixinDemoPage(),
        ),
      ],
    );
  }
}

class StringRes extends Translations {
  @override
  Map<String, Map<String, String>> get keys => {'zh_CN': zh_CN_res, 'en_US': en_US_res};
}

class GetXRouterObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    RouterReportManager.reportCurrentRoute(route);
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) async {
    RouterReportManager.reportRouteDispose(route);
  }
}


// put
// 立即创建并注入实例（单例/永久/可选 tag）。
// lazyPut
// 延迟创建实例，只有第一次使用时才创建（懒加载）。
// putAsync
// 异步创建实例，适合需要异步初始化的对象（如网络、数据库）。
// create
// 每次调用 find 时都会新建一个实例（非单例，每次新建）。
// find
// 获取已注入的实例（通过类型或 tag）。
// delete
// 删除已注入的实例（释放资源，下一次 find 会重新创建）。
// 一句话总结：
// put = 立即，lazyPut = 懒加载，putAsync = 异步，create = 每次新建，find = 获取，delete = 移除。


// class MyApp extends StatelessWidget {
//   const MyApp({super.key});
//   @override
//   Widget build(BuildContext context) {
//     return GetMaterialApp(
//       title: 'GetX Demo',
//       initialRoute: '/',
//       getPages: [
//
//       ],
//     );
//   }
// }



// tab 组件的统一， 加载

// router 组件的统一跳转， 路由站记录路由站历史